package com.ssy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.mapper.RegisterMapper;
import com.ssy.system.pojo.Register;
import com.ssy.system.service.IRegisterService;
import com.ssy.system.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, Register> implements IRegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public PageResult loadAllRegisterPage(RegisterVo registerVo) {
        Page<Register> page = new Page<>(registerVo.getPage(), registerVo.getLimit());
        IPage<Register> registers = this.registerMapper.selectPage(page, new LambdaQueryWrapper<Register>()
                .like(StringUtils.hasText(registerVo.getName()), Register::getName, registerVo.getName())
                .eq(registerVo.getState() != null, Register::getState, registerVo.getState())
                .orderByDesc(Register::getCreateTime));
        return new PageResult(registers.getTotal(), registers.getRecords());
    }

    @Override
    public Result insertRegister(Register register) {
        if (register == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.registerMapper.insert(register);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateRegister(Register register) {
        if (register == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.registerMapper.updateById(register);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteRegisterById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.registerMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteRegister(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.registerMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }

    @Override
    public Result selectAllRegister() {
        List<Register> registers = this.registerMapper.selectList(new LambdaQueryWrapper<Register>()
                .select(Register::getId, Register::getName)
                .eq(Register::getState, Constant.STATE));
        return new Result(registers);
    }
}
